<template>
  <div class="home-container">
     <van-nav-bar fixed class="page-nav-bar">
      <template #title>
        <van-button to="/search" class="search-btn" icon="search" type="primary">搜索</van-button>
      </template>
    </van-nav-bar>
    <van-tabs class="header-tabs" v-model="active" animated swipeable>
      <van-tab v-for="(item) in channels" :key="item.id" :title="item.name">
        <ArticalList :channelsId="item.id"></ArticalList>
      </van-tab>
        <template #nav-right>
           <div @click="isShowEditPopup = true" class="right-btn">
             <i class="toutiao toutiao-gengduo"></i>
           </div>
           <div class="placeholder-box"></div>
        </template>
    </van-tabs>
    <van-popup
      v-model="isShowEditPopup"
      position="bottom"
      :style="{ height: '90%' }"
      closeable
      close-icon-position="top-left"
     >
     <ChannelEdit @update-active="onUpdateActive" :myChannels="channels" :ActiveIndex="active"/>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticalList from './components/ArticalList.vue'
import ChannelEdit from './components/ChannelEdit'
import { Toast } from 'vant'
import { getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },
  components: {
    ArticalList,
    ChannelEdit
  },
  created () {
    this.loadUserChannels()
  },

  methods: {
    async loadUserChannels () {
      try {
        const localChannels = getItem('HMTT-CHANNELS')
        if (!this.$store.state.user && localChannels) {
          // 未登录并且本地有数据
          this.channels = localChannels
        } else {
          const res = await getUserChannels()
          this.channels = res.channels
        }
      } catch (error) {
        Toast('系统异常')
      }
    },
    onUpdateActive (val, isShow) {
      this.active = val
      this.isShowEditPopup = isShow
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container{
    padding-top: 172px;
    /deep/.van-nav-bar__title{
      max-width: unset;
    }
    .search-btn{
      width: 555px;
      height: 64px;
      background: rgba(255, 255, 255, .2);
      border-radius: 32px;
      border: none;
      .van-icon-search{
        color: #fff;
      }
    }
    /deep/.header-tabs{
        .van-tabs__wrap{
        position: fixed;
        top: 92px;
        width: 100%;
        z-index: 999;
        height: 80px;
        border-bottom: 1px solid #EDEFF3;
      }
      .van-tabs__nav--line{
        padding: 0;
      }
      .van-tab{
        width: 200px;
        border-right: 1px solid #EDEFF3;
        .van-tab__text{
          font-size: 28px;
          color: #777;
        }
      }

      .van-tab--active{
        .van-tab__text {
          font-size: 30px;
          color: #333;
        }
      }

      .van-tabs__line{
        width: 31px;
        height: 6px;
        background: #3296FA;
        border-radius: 3px;
        bottom: 8px;
      }
      .right-btn{
        width: 66px;
        height: 80px;
        position: fixed;
        right: 0;
        background-color: rgba(255, 255, 255, .9);
        display: flex;
        justify-content: center;
        align-items: center;
        .toutiao-gengduo{
          font-size: 33px;
        }
        &::before{
          content: '';
          width: 2px;
          height: 58px;
          background-image: url(~@/assets/gradient-gray-line.png);
          background-size: 100% 100%;
          position: absolute;
          left: 0;
        }
      }
      .placeholder-box{
          width: 66px;
          flex-shrink: 0;
      }
    }
  }
</style>
